package cn.com.fuhuitong.main.home.ui.activity

import androidx.lifecycle.Observer
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.base.ViewModeActivity
import cn.com.fuhuitong.main.home.adapter.FVT
import cn.com.fuhuitong.main.home.adapter.FlagshipDetailsAdapter
import cn.com.fuhuitong.main.home.vm.FlagshipViewModel
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import cn.com.fuhuitong.R
import com.alibaba.android.arouter.launcher.ARouter
import kotlinx.android.synthetic.main.activity_home_flagship_details.*

@Route(path = ARouteConstance.ACTIVITY_URL_HOME_FLAGSHIP_DETAILS)
class HomeFlagshipDetailsActivity : ViewModeActivity<FlagshipViewModel>() {


    private lateinit var flagshipDetailsAdapter: FlagshipDetailsAdapter

    override fun viewModel(): FlagshipViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_home_flagship_details

    override fun initView() {
        image_flagship_back.setOnClickListener {
            this.finish()
        }

        text_flagship_shop.setOnClickListener {
            ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_HOME_FLAGSHIP_LIST)
                .navigation()
        }

        this.initViewRecyclerAdapter(
            recycler_flagship_details, arrayOf(FVT.FLAGSHIP_CONTENT.ordinal)
        )

        delegateAdapter.addAdapter(
            FlagshipDetailsAdapter(
                applicationContext, LinearLayoutHelper()
            ).apply {
                flagshipDetailsAdapter = this
            }
        )

    }

    override fun initViewMode() {
        viewModel.configDataLiveData.observe(this, Observer { response ->
            handlerResponseEmpty(
                status = response.status, message = response.statusTip,
                length = if (response.response?.data != null) 1 else 0
            )
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                flagshipDetailsAdapter.setAddDataNew(
                    mutableListOf(it.flagship_store ?: "")
                )
            }
        })
    }

    override fun initData() {
        viewModel.configValue(arrayOf("flagship_store"))
    }

}